package com.dalei.vdsmanager.security.service;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.dalei.vdsmanager.security.dao.Role;
import com.dalei.vdsmanager.security.dao.RoleDao;
import com.dalei.vdsmanager.security.dao.User;
import com.dalei.vdsmanager.security.dao.UserDao;

public class UserServiceImpl implements UserService, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5514384728156903235L;

	private UserDao userDao;

	private RoleDao roleDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public void addUser(String userName, String password, boolean isEnabled,
			User createdBy, Date createdTime, List<Integer> roleIds) {
		// TODO Auto-generated method stub
		User user = new User();
		user.setUser(userName);
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		String encPassword = encoder.encodePassword(password, userName);
		user.setPassword(encPassword);
		user.setIsEnabled(isEnabled);
		user.setCreatedBy(createdBy);
		user.setCreatedTime(createdTime);
		Set<Role> roles = new HashSet<Role>();
		for (Integer roleId : roleIds) {
			if (roleId != null) {
				Role role = roleDao.findRoleById(roleId);
				if (role != null) {
					roles.add(role);
				}
			}
		}
		user.setRoles(roles);
		userDao.saveUser(user);
	}

	@Override
	public boolean checkRepeat(String userName) {
		// TODO Auto-generated method stub
		List<User> users = getUsers();
		for (User u : users) {
			if (u.getUser().equals(userName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return userDao.findUsers();
	}

	@Override
	public User getUserById(int id) {
		// TODO Auto-generated method stub
		return userDao.findUserById(id);
	}

	@Override
	public User getUserByUserName(String userName) {
		// TODO Auto-generated method stub
		return userDao.findUserByUserName(userName);
	}

	@Override
	public boolean changePassword(int id, String newPassword) {
		// TODO Auto-generated method stub
		User user = getUserById(id);
		if (user != null) {
			if (newPassword != null) {
				Md5PasswordEncoder encoder = new Md5PasswordEncoder();
				String encPassword = encoder.encodePassword(newPassword,
						user.getUser());
				user.setPassword(encPassword);
				userDao.updateUser(user);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public void removeUser(int id) {
		// TODO Auto-generated method stub
		User user = getUserById(id);
		if (user != null) {
			userDao.deleteUser(user);
		}
	}

	@Override
	public boolean validateUser(String userName, String password) {
		// TODO Auto-generated method stub
		if (userName != null && password != null) {
			User user = getUserByUserName(userName);
			if (user != null) {
				String encPassword = user.getPassword();
				Md5PasswordEncoder encoder = new Md5PasswordEncoder();
				return encoder.isPasswordValid(encPassword, password, userName);
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}
